function tumorSuppressorNetwork_Fig5AB()
% Creates a protein interaction network for human tumor supressors (TS) and their interacting partners, as found in PFAM database. 
%
% tumorSuppressorNetwork() creates a protein-protein interaction network
% for 22 human tumor supressors (TS) and their interacting partners as found in
% PFAM database. 
% Also, interrogates the net to answer the following questions:
%  - How P53 and BRCA1 relates to each other?
%  - What are common interaction partners of p53 and Rb1?
%
% Node types: TS and a non-TS
% Edges: a domain-domain interaction reported in PFAM.
% See the TS list in createTumorSuppressorsNet()
%
% SYNTAX: [] = tumorSuppressorNetwork()
%
% See also createTumorSuppressorsNet(), createTSConnectionHeatmap_Fig5CD()
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, Jan 2009.


TSnet_PFAM = createTumorSuppressorsNet('http://dasmi.bioinf.mpi-inf.mpg.de/das','ddi',true);

% Question: How P53 and BRCA1 relates to each other?
status = findPath(TSnet_PFAM,'TP53','BRCA1');

% Question: common interaction partners of p53 and Rb1 (these are TS that
% participate in cell cycle checkpoint during G1/S
findCommonInteractions(TSnet_PFAM,'TP53','BRCA1');


function [status] = findPath(graph,labelA,labelB)

idxA = find(strcmp(labelA, get(graph.Nodes,'Label')));
idxB = find(strcmp(labelB, get(graph.Nodes,'Label')));


% Color these blue
set(graph.Nodes(idxA),'Color',[0 0 1],'size',[50 40]);
set(graph.Nodes(idxB),'Color',[0 0 1],'size',[50 40]);

[dist,path,pred] = shortestpath(graph,idxA,idxB,'Directed', false);

if (~isempty(path))
    set(graph.Nodes(path),'Color',[1 0.4 0.4], 'size',[70 40])
    edges = getedgesbynodeid(graph,get(graph.Nodes(path),'ID'));
    set(edges,'LineColor',[1 0 0])
    set(edges,'LineWidth',1.5)

    dolayout(graph);
    view(graph);
    status = 1;
    
else %no path between label A and label B
    status = -1; 
end



function plotSubgraph(sourceGraph,nodeIDs,tsA,tsB)
global tsEntrezID;
sourceGraph.Nodes = getnodesbyid(sourceGraph,nodeIDs);
%sourceGraph.Edges = getedgesbynodeid(sourceGraph,nodeIDs(end-2:end),nodeIDs(1:end-2));

labels = get(sourceGraph.Nodes,'Label');
idxA = find(strcmp(tsA, labels));
idxB = find(strcmp(tsB, labels));

cm = zeros(size(nodeIDs),size(nodeIDs));
cm(idxA,1:end) = 1;
cm(idxB,1:end) = 1;
cm(idxA,idxA) = 0;
cm(idxA,idxB) = 0;
cm(idxB,idxA) = 0;
cm(idxB,idxB) = 0;

%Create network
subGraph = biograph(cm,nodeIDs,'ShowArrows', 'off', 'LayoutType', 'hierarchical'); 


%Customize nodes in the graph
for i=1:size(nodeIDs)
    
    idx = strmatch(subGraph.Nodes(i).ID,tsEntrezID,'exact');
    if(~isempty(idx)) %TS gene
        set(subGraph.Nodes(i),'Color',[1 0 0]);
        set(subGraph.Nodes(i),'LineColor',[1.0000 0.7490 1.0000]);
        set(subGraph.Nodes(i),'LineWidth',1);
        
    else %other genes
        set(subGraph.Nodes(i),'Color',[0 0.6824 0.7529]);
        set(subGraph.Nodes(i),'LineColor',[0 0.8078 0.7529]);
        set(subGraph.Nodes(i),'LineWidth',1);
        set(subGraph.Nodes(i),'Shape','ellipse');
    end
    set(subGraph.Nodes(i),'Label',labels{i});
end

dolayout(subGraph);
view(subGraph);


function [commonLabels,commonIDs]=findCommonInteractions(graph,labelA,labelB)

idxA = find(strcmp(labelA, get(graph.Nodes,'Label')));
idxB = find(strcmp(labelB, get(graph.Nodes,'Label')));

% pANode = find(strcmp(tsEntrezID(idxA), get(graph.Nodes,'ID')));
% pBNode = find(strcmp(tsEntrezID(idxB), get(graph.Nodes,'ID')));

partnersA = getdescendants(graph.nodes(idxA));
partnersB = getdescendants(graph.nodes(idxB));

partnersA_IDs = get(partnersA,'ID');
partnersB_IDs = get(partnersB,'ID');

idxCommon = [];
for i=1:size(partnersB_IDs,1)
   idx = find(strcmp(partnersB_IDs{i}, partnersA_IDs),1);
   if (~isempty(idx))
      idxCommon = [idxCommon idx]; 
   end
end

commonLabels = get(partnersA(idxCommon),'Label');
commonIDs = get(partnersA(idxCommon),'ID');

if (isempty(find(strcmp(num2str(graph.Nodes(idxA).ID),commonIDs),1)) )
    commonIDs = [commonIDs ; num2str(graph.Nodes(idxA).ID)];
end

if (isempty(find(strcmp(num2str(graph.Nodes(idxB).ID),commonIDs),1)) )
    commonIDs = [commonIDs ; num2str(graph.Nodes(idxB).ID)];
end

plotSubgraph(graph,commonIDs,labelA,labelB);


